using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using iPhOwa.Exchange;
using iPhOwa.Exchange.ContentClasses;
using iPhOwa.Exchange.Managers;
using iPhOwa.Web.Controls;

namespace iPhOwa.Web
{
	/// <summary>
	/// Generic page for displaying folder contents.
	/// </summary>
	public partial class ViewFolder : iPhOwaPage
	{
		[SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
		private Uri MainFolder
		{
			get
			{
				string f = Request["f"] ?? "Inbox";

				switch(f.ToLowerInvariant())
				{
					case "calendar":

						return CalendarUrl;

					case "notes":

						return NotesUrl;

					case "tasks":

						return TasksUrl;

					case "contacts":

						return ContactsUrl;

					case "inbox":
					default:

						return InboxUrl;
				}
			}
		}

		/// <summary>
		/// Page load event.
		/// </summary>
		[SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]
		protected void Page_Load(object sender, EventArgs e)
		{
			Uri path = Path ?? MainFolder;
			folderList.Path = path;
			folderList.OwaSession = OwaSession;

			Folder currentFolder = FolderManager.Get(path, OwaSession);

			if(currentFolder == null)
			{
				throw new ApplicationException("Current folder is null!");
			}

			folderNameLabel.Text = currentFolder.DisplayName;
			Title = string.Format(CultureInfo.CurrentUICulture, "{0} - iPhOwa", currentFolder.DisplayName);

			if(currentFolder.ParentName != null)
			{
				Folder parentFolder = FolderManager.Get(currentFolder.ParentName, OwaSession);

				if(parentFolder != null)
				{
					parentNameLabel.Text = parentFolder.DisplayName;
					backPanel.Visible = true;

					backPanel.Href = string.Format(CultureInfo.InvariantCulture, "ViewFolder.aspx?path={0}", parentFolder.Href);
				}
			}

			Control c = GetContentControl(currentFolder.ContentClass);

			iPhOwaControl ipc = c as iPhOwaControl;

			if(ipc != null)
			{
				ipc.Path = path;
				ipc.OwaSession = OwaSession;
			}

			contentPanel.Controls.Add(c);
		}

		private Control GetContentControl(string contentClass)
		{
			switch(contentClass)
			{
				case ContentClassNames.Folder:
				case ContentClassNames.MailFolder:

					return LoadControl("~/Controls/MailboxContent.ascx");

				case ContentClassNames.CalendarFolder:

					return LoadControl("~/Controls/CalendarContent.ascx");

				case ContentClassNames.NoteFolder:

					return LoadControl("~/Controls/NotesContent.ascx");

				case ContentClassNames.TaskFolder:

					return LoadControl("~/Controls/TasksContent.ascx");

				case ContentClassNames.ContactFolder:

					return LoadControl("~/Controls/ContactsContent.ascx");

				default:

					Label l = new Label();
					l.Text = string.Format(CultureInfo.InvariantCulture, "Sorry, I don't know how to handle a {0} folder.", contentClass);

					return l;
			}
		}
	}
}